<template>
	<view class="purchasePlanAdd">
		<uni-nav-bar color="#ffffff" background-color="#00aded" :statusBar="true" title="派车" fixed="true" />
		<view class="purchasePlanAdd_top"></view>
		<view class="purchaseTransOrderAdd_main">
			<dil-Form formId="711" ref="form" v-model="formValue" class="dilForm" labelPosition="left" labelWidth="90px"></dil-Form>
		</view>
		<view class="purchaseTransOrderAdd_floot">
			<button type="warn" @click="cancelForm(formValue)">取消</button>
			<button type="primary" @click="submitGradForm(formValue)">提交</button>
		</view>
	</view>
</template>

<script>
import { mapState, mapMutations } from 'vuex';
export default {
	data() {
		return {
			navBarTitle: '',
			stepOptions: [
				// { title: '主体信息' },
				{ title: '派车信息' }
			],
			stepActive: 0,
			formList: [
				// {
				// 	formId: 528,
				// 	formValue: {}
				// },
				{
					formId: 711,
					formValue: {}
				}
			],
			formValue: {},
			submitFormValue: {
				list: [
					{
						materialNumber: '',
						materialWeight: '',
						driverId: '',
						capacityId: '',
						originDate: '',
						originPoundNo: '',
						originMaterialZjs: ''
					}
				],
				lineId: '',
				planChildId: '',
				loadPointId: '',
				materialId: '',
				transMode: ''
			}
		};
	},
	watch: {
		'formValue.driverId'(val_new, val_old) {
			// this.getCapacityDriver();
			console.log('driverId:' + val_old + '->' + val_new);
			if (val_new) {
				this.getCapacityDriverSelect();
			}
		},
		'formValue.capacityId'(val_new, val_old) {
			// this.getCapacityDriver();
			console.log('capacityId:' + val_old + '->' + val_new);
			if (val_new) {
				this.getCapacityDriverSelect();
			}
		},
		deep: true
	},
	computed: {
		...mapState('sale', ['saleTransOrderItem'])
	},
	onLoad() {
		this.formValue = JSON.parse(JSON.stringify(this.saleTransOrderItem));
		console.log(this.formValue, 'formValue');
		this.formValue.materialWeight = 0;
		this.formValue.materialNumber = 0;
		this.formValue.storageattr = this.saleTransOrderItem.shippingPointId;
	},
	onShow() {
		let pages = getCurrentPages();
		if (pages.length <= 1) {
			this.back();
		}
	},
	methods: {
		...mapMutations('sale', ['updateSaleItem']),
		back() {
			// #ifdef APP-PLUS
			uni.navigateBack();
			// #endif

			// #ifdef H5
			this.$router.back();
			// #endif
		},
		//步骤，上一步
		previous(index, form) {
			this.formValue = form;
			this.stepActive = index;
		},
		next(index, form) {
			this.formValue = form;
			this.stepActive = index;
		},
		//取消事件
		cancelForm(form) {
			this.formValue = form;
			this.back();
			uni.showToast({
				title: '用户取消操作',
				duration: 2000
			});
		},
		//提交事件
		async submitGradForm(form) {
			console.log('form: ', form);
			this.formValue = form;
			uni.showLoading({
				title: '派车中，请等待'
			});
			//车辆，无则新增
			const capacityRes = await this.$myRequest({
				url: '/api/v1/rms/insertCapacity',
				data: this.formValue
			}).catch((e) => {
				uni.showToast({
					duration: 2000,
					title: e,
					icon: 'none'
				});
				uni.hideLoading();
			});
			this.formValue.capacityId = capacityRes.data.data.capacityId;
			//司机，无则新增，有则修改手机号、身份证
			const driverRes = await this.$myRequest({
				url: '/api/v1/rms/insertDriver',
				data: this.formValue
			}).catch((e) => {
				uni.showToast({
					duration: 2000,
					title: e,
					icon: 'none'
				});
				uni.hideLoading();
			});
			this.formValue.driverId = driverRes.data.data.personnelId;
			let map = this.formValue;
			map.list = [JSON.parse(JSON.stringify(form))];
			const res = await this.$myRequest({
				url: '/api/v1/tms/saleDispatchOrderList',
				data: map
			}).catch((e) => {
				uni.showToast({
					duration: 2000,
					title: e,
					icon: 'none'
				});
				uni.hideLoading();
			});
			if (res.data.status == 'succeed') {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'none'
				});
				setTimeout(() => {
					this.back();
				}, 2100);
			} else {
				uni.showToast({
					title: res.data.message,
					duration: 2000,
					icon: 'none'
				});
			}
			uni.hideLoading();
		},
		async getCapacityDriver() {
			if (typeof this.formValue.capacityId == 'undefined' || typeof this.formValue.driverId == 'undefined') {
				const res = await this.$myRequest({
					url: '/api/v1/uc/getCapacityDriver',
					data: this.formValue
				}).catch((e) => {
					uni.showToast({
						duration: 2000,
						title: e,
						icon: 'none'
					});
				});
				if (res.data.status == 'succeed') {
					if (typeof this.formValue.capacityId == 'undefined') {
						this.formValue.capacityId = res.data.data.capacityId;
					}
					if (typeof this.formValue.driverId == 'undefined') {
						this.formValue.driverId = res.data.data.driverId;
					}
				}
			}
		},
		async getCapacityDriverSelect() {
			if (typeof this.formValue.capacityId == 'undefined' || typeof this.formValue.driverId == 'undefined') {
				const res = await this.$myRequest({
					url: '/api/v1/uc/getCapacityDriverSelect',
					data: this.formValue
				}).catch((e) => {
					uni.showToast({
						duration: 2000,
						title: e,
						icon: 'none'
					});
				});
				if (res.data.status == 'succeed') {
					//选择车辆
					if (typeof this.formValue.capacityId == 'undefined' && this.formValue.driverId) {
						let _this = this;
						let itemList = res.data.data;
						let labelList = [];
						itemList.forEach((item) => {
							labelList.push(item.capacityNumber);
						});
						uni.showActionSheet({
							itemList: labelList,
							success(res) {
								_this.$set(_this.formValue, 'capacityId', itemList[res.tapIndex].capacityId);
							},
							fail(res) {
								console.log(res.errMsg);
							}
						});
					}
					//选择司机
					if (typeof this.formValue.driverId == 'undefined' && this.formValue.capacityId) {
						let _this = this;
						let itemList = res.data.data;
						let labelList = [];
						itemList.forEach((item) => {
							labelList.push(item.driverName);
						});
						uni.showActionSheet({
							itemList: labelList,
							success(res) {
								_this.$set(_this.formValue, 'driverId', itemList[res.tapIndex].driverId);
							},
							fail(res) {
								console.log(res.errMsg);
							}
						});
					}
				}
			}
		}
	}
};
</script>
<style scoped lang="scss">
.purchaseTransOrderAdd {
	&_top {
	}
	&_main {
		margin-top: 40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-direction: column;
	}
	&_floot {
		display: flex;
		justify-content: center;
		align-items: center;
	}
}
</style>
